﻿using System;
using System.Collections;
using System.Reflection;

namespace QAHelper.Common
{
    public static class CommonHelper
    {
        private static ArrayList GetDataSetHeader(string[] raw_data_set, string delim_start, string delim_end)
        {
            ArrayList data_set_headers = new ArrayList();
            foreach (string raw_string in raw_data_set)
            {
                if (!string.IsNullOrEmpty(raw_string))
                {
                    if (raw_string.Contains(delim_start) && raw_string.Contains(delim_end))
                    {
                        data_set_headers.Add(raw_string.Substring(delim_start.Length,raw_string.LastIndexOf(delim_end)-delim_end.Length));
                    }
                }
            }

            return data_set_headers;
        }

        public static DictionaryEntry[] GetFormattedDataSet(string raw, out ArrayList headers, string delim_start, string delim_end, string data_end_delim)
        {            
            string[] splitted_raw_string = raw.Split('\n');
            headers = new ArrayList();
            ArrayList tempData = new ArrayList();
            ArrayList dataTotReturn = new ArrayList();
            headers = GetDataSetHeader(splitted_raw_string, delim_start, delim_end);
            int index = 0;
            int raw_string_index = 0;
            DictionaryEntry[] dataSetCollection = new DictionaryEntry[headers.Count];

            foreach (string str in splitted_raw_string)
            {
                if (!string.IsNullOrEmpty(str))
                {
                    if (!str.Contains(delim_end) && !str.Contains(delim_start) && !str.Contains(data_end_delim))
                    {
                        try
                        {
                            string[] temp = str.Split('>');
                            tempData.Add(new DictionaryEntry(temp[0], temp[1]));
                        }
                        catch (Exception exp)
                        {

                            throw;
                        }
                    }
                    else
                    {
                        if (raw_string_index > 0)
                        {
                            if (!str.Contains(data_end_delim))
                            {
                                ArrayList tempArr = new ArrayList();
                                int i=0;
                                foreach (DictionaryEntry dict in tempData)
                                {
                                    tempArr.Add(dict);
                                    i++;
                                }

                                dataSetCollection[index] = new DictionaryEntry(headers[index],tempArr);
                                index++;
                                tempData.Clear();
                            }
                        }
                    }
                }
                raw_string_index++;
            }

            if (tempData.Count > 0)
            {
                ArrayList tempArr = new ArrayList();
                int i = 0;
                foreach (DictionaryEntry dict in tempData)
                {
                    tempArr.Add(dict);
                    i++;
                }

                dataSetCollection[index] = new DictionaryEntry(headers[index], tempArr);
                tempData.Clear();
            }

            return dataSetCollection;
        }

        public static ArrayList GetDataFromDictionary(DictionaryEntry[] dataSet, int index)
        {
            DictionaryEntry tempDict = dataSet[index];

            return (ArrayList)tempDict.Value;
        }

        public static string GetRawTestData(int testDataColumnNo, int rowIndex, ArrayList testScenarios)
        {
            DictionaryEntry[] testcase = (DictionaryEntry[])testScenarios[rowIndex];
            return testcase[Convert.ToInt16(testDataColumnNo)].Value.ToString();
        }

        /// <summary>
        /// Returns the expected data set for a test scenario.
        /// </summary>
        /// <param name="startDelimeter"></param>
        /// <param name="endDelimeter"></param>
        /// <param name="dataSetDelimeter"></param>
        /// <param name="rawData"></param>
        /// <returns></returns>
        public static ArrayList GetExpectedDataSet(string startDelimeter, string endDelimeter, string dataSetDelimeter, string rawData)
        {
            string[] splittedRawData = rawData.Split('\n');
            string[] tempDataset = null;
            ArrayList dataSet = new ArrayList();
            int index = 0;
            bool flag = false;
            
            for (index = 0; index < splittedRawData.Length; index++)
            {
                if (splittedRawData[index].Contains("[%"))
                {
                    flag = true;
                    break;
                }
            }

            if (flag && ((index + 1) < splittedRawData.Length))
            {
                tempDataset = splittedRawData[index + 1].Split('|');
                foreach (string temp in tempDataset)
                    dataSet.Add(temp.Trim());
            }

            return dataSet;
        }

        public static object GetInvokerType(string nameSpace, string className, out Type testType, TestCaseStructure testCaseStructure, ArrayList headerList)
        {
            Assembly testAssembly = Assembly.Load(nameSpace);
            testType = testAssembly.GetType(nameSpace + "." + className);
            object testObj = Activator.CreateInstance(testType, new object[] {testCaseStructure,headerList});
            return testObj;
        }

        public static bool InvokeTestFunction(string testMethodName, Type typeOfClass, object testObj, TestScenario scenario)
        {
            return (bool)typeOfClass.InvokeMember(testMethodName, BindingFlags.Default | BindingFlags.InvokeMethod, null, testObj, new object[] { scenario}); 
        }
    }
}
